package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.MesOrderMaterialRpcService;
import com.hunttown.mes.rpc.domain.MesOrderMaterialDTO;
import com.hunttown.mes.rpc.domain.query.MesOrderMaterialDTOQuery;
import com.hunttown.mes.service.MesOrderMaterialService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.MesOrderMaterialConverts.convert;

/**
 * created by wangjunfu on 2022-03-30 15:27:22
 */
@Service
public class MesOrderMaterialRpcServiceImpl implements MesOrderMaterialRpcService {

    private final Logger logger = LoggerFactory.getLogger(MesOrderMaterialRpcServiceImpl.class);

    private final MesOrderMaterialService mesOrderMaterialService;

    @Autowired
    public MesOrderMaterialRpcServiceImpl(MesOrderMaterialService mesOrderMaterialService) {
        this.mesOrderMaterialService = mesOrderMaterialService;
    }

    @Override
    public MesOrderMaterialDTO insert(MesOrderMaterialDTO mesOrderMaterialDTO) {
        try {
            return convert(mesOrderMaterialService.insert(convert(mesOrderMaterialDTO)));
        } catch (Exception e) {
            logger.error("MesOrderMaterial create failed.", e);
            throw new RuntimeException("MesOrderMaterial create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(MesOrderMaterialDTO mesOrderMaterialDTO) {
        try {
            return mesOrderMaterialService.updateInfoById(convert(mesOrderMaterialDTO));
        } catch (Exception e) {
            logger.error("MesOrderMaterial update by id failed.", e);
            throw new RuntimeException("MesOrderMaterial update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(MesOrderMaterialDTOQuery mesOrderMaterialDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (mesOrderMaterialDTOQuery.getW_id() != null && mesOrderMaterialDTOQuery.getW_id() > 0) {
                query.put("w_id", mesOrderMaterialDTOQuery.getW_id());
            }

            if (mesOrderMaterialDTOQuery.getW_tableId() != null && mesOrderMaterialDTOQuery.getW_tableId() > 0) {
                query.put("w_tableId", mesOrderMaterialDTOQuery.getW_tableId());
            }

            if (mesOrderMaterialDTOQuery.getW_recordId() != null && mesOrderMaterialDTOQuery.getW_recordId() > 0) {
                query.put("w_recordId", mesOrderMaterialDTOQuery.getW_recordId());
            }

            if (mesOrderMaterialDTOQuery.getIds() != null && !mesOrderMaterialDTOQuery.getIds().equals("")) {
                query.put("ids", mesOrderMaterialDTOQuery.getIds());
            }

            if (mesOrderMaterialDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", mesOrderMaterialDTOQuery.getDeleteFlag());
            }

            return mesOrderMaterialService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("MesOrderMaterial update by query failed.", e);
            throw new RuntimeException("MesOrderMaterial update by query failed, Please order your code.", e);
        }
    }

    @Override
    public MesOrderMaterialDTO selectById(Integer id) {
        try {
            return convert(mesOrderMaterialService.selectById(id));
        } catch (Exception e) {
            logger.error("MesOrderMaterial select by id failed.", e);
            throw new RuntimeException("MesOrderMaterial select by id failed, Please order your code.", e);
        }
    }

    @Override
    public MesOrderMaterialDTO selectObjByQuery(MesOrderMaterialDTOQuery mesOrderMaterialDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (mesOrderMaterialDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", mesOrderMaterialDTOQuery.getDeleteFlag());
            }

            if (StringUtils.isNotBlank(mesOrderMaterialDTOQuery.getOrderBy())) {
                query.put("orderBy", mesOrderMaterialDTOQuery.getOrderBy());
            }

            return convert(mesOrderMaterialService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("MesOrderMaterial select obj by query failed.", e);
            throw new RuntimeException("MesOrderMaterial select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<MesOrderMaterialDTO> getListForPage(MesOrderMaterialDTOQuery mesOrderMaterialDTOQuery) {
        Query query = Query.page(mesOrderMaterialDTOQuery.getPn(), mesOrderMaterialDTOQuery.getPageSize());

        if (mesOrderMaterialDTOQuery.getId() != null && mesOrderMaterialDTOQuery.getId() > 0) {
            query.put("id", mesOrderMaterialDTOQuery.getId());
        }

        if (mesOrderMaterialDTOQuery.getIds() != null && !mesOrderMaterialDTOQuery.getIds().equals("")) {
            query.put("ids", mesOrderMaterialDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(mesOrderMaterialDTOQuery.getLikeName())) {
            query.put("likeName", mesOrderMaterialDTOQuery.getLikeName());
        }

        if (mesOrderMaterialDTOQuery.getTableId() != null) {
            query.put("tableId", mesOrderMaterialDTOQuery.getTableId());
        }

        if (mesOrderMaterialDTOQuery.getRecordId() != null) {
            query.put("recordId", mesOrderMaterialDTOQuery.getRecordId());
        }

        if (StringUtils.isNotBlank(mesOrderMaterialDTOQuery.getShowDate())) {
            query.put("showDate", mesOrderMaterialDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(mesOrderMaterialDTOQuery.getStartDate())) {
            query.put("startDate", mesOrderMaterialDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(mesOrderMaterialDTOQuery.getEndDate())) {
            query.put("endDate", mesOrderMaterialDTOQuery.getEndDate());
        }

        if (mesOrderMaterialDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", mesOrderMaterialDTOQuery.getDeleteFlag());
        }

        if (StringUtils.isNotBlank(mesOrderMaterialDTOQuery.getOrderBy())) {
            query.put("orderBy", mesOrderMaterialDTOQuery.getOrderBy());
        }

        List<MesOrderMaterialDTO> list = convert(mesOrderMaterialService.selectByQuery(query));
        Page<MesOrderMaterialDTO> page = Page.from(mesOrderMaterialDTOQuery.getPn(), mesOrderMaterialDTOQuery.getPageSize(), list);
        page.setTotalItems(mesOrderMaterialService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return mesOrderMaterialService.deleteById(id);
        } catch (Exception e) {
            logger.error("MesOrderMaterial delete failed.", e);
            throw new RuntimeException("MesOrderMaterial delete failed, Please order your code.", e);
        }
    }
}